{% extends 'base.html' %}
{% load static %}
{% load user_tags %}

  {% block title %}
  <title>在线终端</title>
  {% endblock title %}

	{% block navheader %}
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-1">
          <div class="col-12">
            <ol class="breadcrumb">
              <li class="breadcrumb-item">远程终端</li>
              <li class="breadcrumb-item active">在线终端</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>
	{% endblock navheader %}
	
		  {% block content %}
          <div class="card content-list">
            <div class="card-header">
              <h3 class="card-title">在线终端</h3>
				<div class="card-tools">
				  <!--button type="button" class="btn btn-tool" data-widget="collapse">
					<i class="fas fa-minus"></i>
				  </button>
				  <button type="button" class="btn btn-tool" data-widget="maximize">
				  <i class="fas fa-expand"></i>
                  </button>
				  <button type="button" class="btn btn-tool" data-widget="remove">
					<i class="fas fa-times"></i>
				  </button-->
				</div>
            </div>
            <!-- /.card-header -->
            <div class="card-body table-responsive">
              <table id="datatables-lists" class="table table-bordered table-hover">
                <thead>
                <tr>
				  <th>ID</th>
				  <th>会话名称</th>
				  <th>用户</th>
				  <th>类型</th>
				  <th>连接</th>
				  <th>协议</th>
				  <th>客户端IP</th>
				  <th>客户端</th>
				  <th>开始时间</th>
				  <th>操作</th>
                </tr>
                </thead>
                <tbody>
				{% for session in sessions %}
                <tr id="session-{{ session.id }}">
				  <td>{{ session.id }}</td>
				  <td>{{ session.name }}</td>
				  <td>{{ session.user }}</td>
				  <td>
				  {% if session.get_type_display == 'webssh' %}
					<span class="badge badge-success">{{ session.get_type_display }}</span>
				  {% elif session.get_type_display == 'websftp' %}
					<span class="badge badge-info">{{ session.get_type_display }}</span>
				  {% elif session.get_type_display == 'webtelnet' %}
					<span class="badge badge-primary">{{ session.get_type_display }}</span>
				  {% elif session.get_type_display == 'clissh' %}
					<span class="badge badge-warning">{{ session.get_type_display }}</span>
				  {% elif session.get_type_display == 'clisftp' %}
					<span class="badge badge-secondary">{{ session.get_type_display }}</span>
				  {% else %}
				    <span class="badge badge-danger">{{ session.get_type_display }}</span>
				  {% endif %}
				  </td>
				  <td>{{ session.username }}@{{ session.host }}:{{ session.port }}</td>
				  <td>
				  {% if session.get_protocol_display == 'ssh' %}
					<span class="badge badge-success">{{ session.get_protocol_display }}</span>
				  {% elif session.get_protocol_display == 'sftp' %}
					<span class="badge badge-info">{{ session.get_protocol_display }}</span>
				  {% elif session.get_protocol_display == 'telnet' %}
					<span class="badge badge-primary">{{ session.get_protocol_display }}</span>
				  {% else %}
				    <span class="badge badge-secondary">{{ session.get_protocol_display }}</span>
				  {% endif %}
				  </td>
				  
				  <td>{{ session.address | default:'' }}</td>
				  <td>{{ session.useragent | truncatechars_html:25 | default:'' }}...</td>
				  <td>{{ session.create_time | date:"Y/m/d H:i:s" }}</td>
				  
				  <td>
					{% if request|has_permission:'终端会话查看' %}
						{% if session.get_type_display == 'webssh' or session.get_type_display == 'webtelnet' %}
							<form method="post" action="{% url 'webssh:terminal_view' %}" target="_blank" style="display:inline;">
							{% csrf_token %}
							<input type="text" name="sessionname" value="{{ session.name  }}" hidden>
							<input type="text" name="group" value="{{ session.group  }}" hidden>
							<button type="submit" class="btn btn-sm btn-success mb-1"><i class="far fa-eye"></i> 查看</button>
							</form>&nbsp;&nbsp;
						{% elif session.get_type_display == 'clissh' %}
							<form method="post" action="{% url 'webssh:terminal_clissh_view' %}" target="_blank" style="display:inline;">
							{% csrf_token %}
							<input type="text" name="sessionname" value="{{ session.name  }}" hidden>
							<input type="text" name="group" value="{{ session.group  }}" hidden>
							<button type="submit" class="btn btn-sm btn-success mb-1"><i class="far fa-eye"></i> 查看</button>
							</form>&nbsp;&nbsp;
						{% endif %}
					{% endif %}
					
					{% if request|has_permission:'终端会话关闭' %}
						{% if session.get_type_display == 'webssh' or session.get_type_display == 'webtelnet' %}
							<a href="javascript:void(0)" id="{{ session.id }}" title="{{ session.name }}" group="{{ session.group }}" class="btn btn-sm btn-danger mb-1" onclick="createdeletemodal(this);"><i class="far fa-times-circle"></i> 断开</a>
							&nbsp;&nbsp;
						{% elif session.get_type_display == 'clissh' or session.get_type_display == 'clisftp' %}
							<a href="javascript:void(0)" id="{{ session.id }}" title="{{ session.name }}" class="btn btn-sm btn-danger mb-1" onclick="createdeletemodal_clissh(this);"><i class="far fa-times-circle"></i> 断开</a>
							&nbsp;&nbsp;
						{% elif session.get_type_display == 'webrdp' or session.get_type_display == 'webvnc' %}
							<a href="javascript:void(0)" id="{{ session.id }}" title="{{ session.name }}" group="{{ session.group }}" class="btn btn-sm btn-danger mb-1" onclick="createdeletemodal_rdp(this);"><i class="far fa-times-circle"></i> 断开</a>
							&nbsp;&nbsp;
						{% endif %}
					{% endif %}
					
					{% if request|has_permission:'终端会话锁定/解锁' %}
						{% if session.get_type_display == 'webssh' or session.get_type_display == 'webtelnet' or session.get_type_display == 'webrdp' or session.get_type_display == 'webvnc' or session.get_type_display == 'clissh' %}
							{% if session.locked %}
							<a href="javascript:void(0)" id="{{ session.id }}" title="{{ session.name }}" type="{{ session.get_type_display }}" class="btn btn-sm btn-primary mb-1" onclick="createunlockmodal(this);"><i class="fas fa-lock-open"></i> 解锁</a>
							{% else %}
							<a href="javascript:void(0)" id="{{ session.id }}" title="{{ session.name }}" type="{{ session.get_type_display }}" class="btn btn-sm btn-info mb-1" onclick="createlockmodal(this);"><i class="fas fa-lock"></i> 锁定</a>
							{% endif %}
						{% endif %}
					{% endif %}
					
				  </td>
                </tr>
				{% endfor %}
                </tbody>
                <tfoot>
                <tr>
				  <th>ID</th>
				  <th>会话名称</th>
				  <th>用户</th>
				  <th>类型</th>
				  <th>连接</th>
				  <th>协议</th>
				  <th>客户端IP</th>
				  <th>客户端</th>
				  <th>开始时间</th>
				  <th>操作</th>
                </tr>
                </tfoot>
              </table>
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
		  
		  <!--关闭会话确认窗口-->
		  <div id="modal-container">
		  </div>
		  
		  {% endblock content %}

{% block js %}
<!-- DataTables -->
<script src="{% static 'adminlte/plugins/datatables/jquery.dataTables.js' %}"></script>
<script src="{% static 'adminlte/plugins/datatables/dataTables.bootstrap4.js' %}"></script>
<script>
	$("#datatables-lists").DataTable({
		language: {
			"sProcessing": "处理中...",
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sZeroRecords": "没有匹配结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
			"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
			"sInfoPostFix": "",
			"sSearch": "搜索:",
			"sUrl": "",
			"sEmptyTable": "无数据",
			"sLoadingRecords": "载入中...",
			"sInfoThousands": ",",
			"oPaginate": {
				"sFirst": "首页",
				"sPrevious": "上页",
				"sNext": "下页",
				"sLast": "末页"
			},
			"oAria": {
				"sSortAscending": ": 以升序排列此列",
				"sSortDescending": ": 以降序排列此列"
			}
		},
		destroy: true,	// 允许重建
		bProcessing:true,  // 表格数据过多处理时显示: sProcessing
		lengthMenu: [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "全部"]],
		columnDefs : [ { targets : 9, "orderable" : false } ],
		order: [],
		 //scrollY: 480,	// 滚动条
		 //scrollCollapse: true,
		 //jQueryUI: true,
		 stateSave: true,	// 保存最后一次分页信息、排序信息，当页面刷新，或者重新进入这个页面，恢复上次的状态。
		 stateDuration: 86400,	// 本地储存(0~更大)还是session储存(-1) 
	});

	// 关闭webssh会话 - start
	createdeletemodal = function (event) {
		var thisObj = $(event);	//js对象转jquery对象  
		var session_id = thisObj.attr('id');
		var session_name = thisObj.attr("title");
		var session_group = thisObj.attr("group");
		// 生成弹出框，使用 \ 换行
		$("#modal-container").html('<div id="modal-delete-' + session_id + '" class="iziModal"><div class="container-fluid"><div class="row">\
									<div class="col-12 p-3"><h5 class="text-center mt-3">确定要强制断开会话 "' + session_name + '" 吗？</h5></div><div class="col-6 p-3">\
									<button type="button" class="btn btn-block btn-secondary btn-flat" data-iziModal-close>取消</button></div><div class="col-6 p-3">\
									<a href="javascript:void(0)"><button type="button" class="btn btn-block btn-success btn-flat" id="' + session_id + '-yes" title="\
									' + session_name + '" group="' + session_group + '" onclick="deletethis(this);">确定</button></a></div></div></div></div>');
		
		// 初始化弹出框
		$("#modal-delete-" + session_id).iziModal({
			iconClass: 'icon-announcement',
			width: 450,
			//padding: 10,
			overlayClose: false,	// 是否允许点击模态窗口的外部来关闭模态窗口。
			closeOnEscape: false, 	// 是否允许通过点击ESC键来关闭模态窗口。
		});
				
		// 打开弹出框
		$('#modal-delete-' + session_id).iziModal('open');
	}
	
		
	deletethis = function(event) {
		toastr.options.closeButton = true;
		toastr.options.showMethod = 'slideDown';
		toastr.options.hideMethod = 'fadeOut';
		toastr.options.closeMethod = 'fadeOut';
		toastr.options.timeOut = 4000;	
		toastr.options.extendedTimeOut = 0;	
		
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id').split("-")[0];
		var session_name = thisObj.attr("title");
		var session_group = thisObj.attr("group");
		csrfmiddlewaretoken = '{{ request.COOKIES.csrftoken }}';
		$.ajax({
			url: "{% url 'webssh_api:session_close' %}",
			async: true,
			type: 'POST',
			dataType: 'json',
			data: {
				'csrfmiddlewaretoken': csrfmiddlewaretoken,
				'id': session_id,
				'group': session_group,
			},
			timeout: 10000,
			cache: true,
			beforeSend: LoadFunction, //加载执行方法
			error: errFunction,  //错误执行方法
			success: succFunction, //成功执行方法
		});
		
		function LoadFunction() {
			//$("#aboutcontent").html('删除中...');
			//alert('删除中');
		};
		
		function errFunction() {
			// 关闭弹出框
			$('#modal-delete-' + session_id).iziModal('close');
			// 消息框
			toastr.error('关闭会话错误 "' + session_name + '"');
		};
		
		function succFunction(res) {
			if (res.code != 200) {
				// 关闭弹出框
				$('#modal-delete-' + session_id).iziModal('close');
				// 消息框
				toastr.error('关闭会话错误 "' + session_name + '": ' + res.err);
			} else {
				// 先销毁 datatable , 动态删除数据后，然后再重建
				var dttable = $('#datatables-lists').DataTable();
				dttable.destroy();
				
				$("#session-" + session_id).remove();
				
				$("#datatables-lists").DataTable({
					language: {
						"sProcessing": "处理中...",
						"sLengthMenu": "显示 _MENU_ 项结果",
						"sZeroRecords": "没有匹配结果",
						"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
						"sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
						"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
						"sInfoPostFix": "",
						"sSearch": "搜索:",
						"sUrl": "",
						"sEmptyTable": "无数据",
						"sLoadingRecords": "载入中...",
						"sInfoThousands": ",",
						"oPaginate": {
							"sFirst": "首页",
							"sPrevious": "上页",
							"sNext": "下页",
							"sLast": "末页"
						},
						"oAria": {
							"sSortAscending": ": 以升序排列此列",
							"sSortDescending": ": 以降序排列此列"
						}
					},
					destroy: true,	// 允许重建
					bProcessing:true,  // 表格数据过多处理时显示: sProcessing
					lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "全部"]],
					columnDefs : [ { targets : 9, "orderable" : false } ],
					order: [],
					 //scrollY: 480,	// 滚动条
					 //scrollCollapse: true,
					 //jQueryUI: true,
					 stateSave: true,	// 保存最后一次分页信息、排序信息，当页面刷新，或者重新进入这个页面，恢复上次的状态。
					 stateDuration: 86400,	// 本地储存(0~更大)还是session储存(-1) 
				});
				
				// 关闭弹出框
				$('#modal-delete-' + session_id).iziModal('close');
				
				// 消息框
				toastr.success('成功关闭会话 "' + session_name + '"');
			}
		};
	}
	// 关闭webssh会话 - end
	
	// 关闭clissh会话 - start
	createdeletemodal_clissh = function (event) {
		var thisObj = $(event);	//js对象转jquery对象  
		var session_id = thisObj.attr('id');
		var session_name = thisObj.attr("title");
		// 生成弹出框，使用 \ 换行
		$("#modal-container").html('<div id="modal-delete-' + session_id + '" class="iziModal"><div class="container-fluid"><div class="row">\
									<div class="col-12 p-3"><h5 class="text-center mt-3">确定要强制关闭会话 "' + session_name + '" 吗？</h5></div><div class="col-6 p-3">\
									<button type="button" class="btn btn-block btn-secondary btn-flat" data-iziModal-close>取消</button></div><div class="col-6 p-3">\
									<a href="javascript:void(0)"><button type="button" class="btn btn-block btn-success btn-flat" id="' + session_id + '-yes" title="\
									' + session_name + '" onclick="deleteclisshthis(this);">确定</button></a></div></div></div></div>');
		
		// 初始化弹出框
		$("#modal-delete-" + session_id).iziModal({
			iconClass: 'icon-announcement',
			width: 450,
			//padding: 10,
			overlayClose: false,	// 是否允许点击模态窗口的外部来关闭模态窗口。
			closeOnEscape: false, 	// 是否允许通过点击ESC键来关闭模态窗口。
		});
				
		// 打开弹出框
		$('#modal-delete-' + session_id).iziModal('open');
	}
	
		
	deleteclisshthis = function(event) {
		toastr.options.closeButton = true;
		toastr.options.showMethod = 'slideDown';
		toastr.options.hideMethod = 'fadeOut';
		toastr.options.closeMethod = 'fadeOut';
		toastr.options.timeOut = 4000;	
		toastr.options.extendedTimeOut = 0;	
		
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id').split("-")[0];
		var session_name = thisObj.attr("title");
		csrfmiddlewaretoken = '{{ request.COOKIES.csrftoken }}';
		$.ajax({
			url: "{% url 'webssh_api:session_clissh_close' %}",
			async: true,
			type: 'POST',
			dataType: 'json',
			data: {
				'csrfmiddlewaretoken': csrfmiddlewaretoken,
				'id': session_id,
				'session': session_name,
			},
			timeout: 10000,
			cache: true,
			beforeSend: LoadFunction, //加载执行方法
			error: errFunction,  //错误执行方法
			success: succFunction, //成功执行方法
		});
		
		function LoadFunction() {
			//$("#aboutcontent").html('删除中...');
			//alert('删除中');
		};
		
		function errFunction() {
			// 关闭弹出框
			$('#modal-delete-' + session_id).iziModal('close');
			// 消息框
			toastr.error('关闭会话错误 "' + session_name + '"');
		};
		
		function succFunction(res) {
			if (res.code != 200) {
				// 关闭弹出框
				$('#modal-delete-' + session_id).iziModal('close');
				// 消息框
				toastr.error('关闭会话错误 "' + session_name + '": ' + res.err);
			} else {
				// 先销毁 datatable , 动态删除数据后，然后再重建
				var dttable = $('#datatables-lists').DataTable();
				dttable.destroy();
				
				$("#session-" + session_id).remove();
				
				$("#datatables-lists").DataTable({
					language: {
						"sProcessing": "处理中...",
						"sLengthMenu": "显示 _MENU_ 项结果",
						"sZeroRecords": "没有匹配结果",
						"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
						"sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
						"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
						"sInfoPostFix": "",
						"sSearch": "搜索:",
						"sUrl": "",
						"sEmptyTable": "无数据",
						"sLoadingRecords": "载入中...",
						"sInfoThousands": ",",
						"oPaginate": {
							"sFirst": "首页",
							"sPrevious": "上页",
							"sNext": "下页",
							"sLast": "末页"
						},
						"oAria": {
							"sSortAscending": ": 以升序排列此列",
							"sSortDescending": ": 以降序排列此列"
						}
					},
					destroy: true,	// 允许重建
					bProcessing:true,  // 表格数据过多处理时显示: sProcessing
					lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "全部"]],
					order: [],
					 //scrollY: 480,	// 滚动条
					 //scrollCollapse: true,
					 //jQueryUI: true,
					 stateSave: true,	// 保存最后一次分页信息、排序信息，当页面刷新，或者重新进入这个页面，恢复上次的状态。
					 stateDuration: 86400,	// 本地储存(0~更大)还是session储存(-1) 
				});
				
				// 关闭弹出框
				$('#modal-delete-' + session_id).iziModal('close');
				
				// 消息框
				toastr.success('成功关闭会话 "' + session_name + '"');
			}
		};
	}
	// 关闭clissh会话 - end


	// 关闭rdp&vnc会话  - start
	createdeletemodal_rdp = function (event) {
		var thisObj = $(event);	//js对象转jquery对象  
		var session_id = thisObj.attr('id');
		var session_name = thisObj.attr("title");
		var session_group = thisObj.attr("group");
		// 生成弹出框，使用 \ 换行
		$("#modal-container").html('<div id="modal-delete-' + session_id + '" class="iziModal"><div class="container-fluid"><div class="row">\
									<div class="col-12 p-3"><h5 class="text-center mt-3">确定要强制关闭会话 "' + session_name + '" 吗？</h5></div><div class="col-6 p-3">\
									<button type="button" class="btn btn-block btn-secondary btn-flat" data-iziModal-close>取消</button></div><div class="col-6 p-3">\
									<a href="javascript:void(0)"><button type="button" class="btn btn-block btn-success btn-flat" id="' + session_id + '-yes" title="\
									' + session_name + '" group="' + session_group + '" onclick="deleterdpthis(this);">确定</button></a></div></div></div></div>');
		
		// 初始化弹出框
		$("#modal-delete-" + session_id).iziModal({
			iconClass: 'icon-announcement',
			width: 450,
			//padding: 10,
			overlayClose: false,	// 是否允许点击模态窗口的外部来关闭模态窗口。
			closeOnEscape: false, 	// 是否允许通过点击ESC键来关闭模态窗口。
		});
				
		// 打开弹出框
		$('#modal-delete-' + session_id).iziModal('open');
	}
	
		
	deleterdpthis = function(event) {
		toastr.options.closeButton = true;
		toastr.options.showMethod = 'slideDown';
		toastr.options.hideMethod = 'fadeOut';
		toastr.options.closeMethod = 'fadeOut';
		toastr.options.timeOut = 4000;	
		toastr.options.extendedTimeOut = 0;	
		
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id').split("-")[0];
		var session_name = thisObj.attr("title");
		var session_group = thisObj.attr("group");
		csrfmiddlewaretoken = '{{ request.COOKIES.csrftoken }}';
		$.ajax({
			url: "{% url 'webssh_api:session_rdp_close' %}",
			async: true,
			type: 'POST',
			dataType: 'json',
			data: {
				'csrfmiddlewaretoken': csrfmiddlewaretoken,
				'id': session_id,
				'group': session_group,
			},
			timeout: 10000,
			cache: true,
			beforeSend: LoadFunction, //加载执行方法
			error: errFunction,  //错误执行方法
			success: succFunction, //成功执行方法
		});
		
		function LoadFunction() {
			//$("#aboutcontent").html('删除中...');
			//alert('删除中');
		};
		
		function errFunction() {
			// 关闭弹出框
			$('#modal-delete-' + session_id).iziModal('close');
			// 消息框
			toastr.error('关闭会话错误 "' + session_name + '"');
		};
		
		function succFunction(res) {
			if (res.code != 200) {
				// 关闭弹出框
				$('#modal-delete-' + session_id).iziModal('close');
				// 消息框
				toastr.error('关闭会话错误 "' + session_name + '": ' + res.err);
			} else {
				// 先销毁 datatable , 动态删除数据后，然后再重建
				var dttable = $('#datatables-lists').DataTable();
				dttable.destroy();
				
				$("#session-" + session_id).remove();
				
				$("#datatables-lists").DataTable({
					language: {
						"sProcessing": "处理中...",
						"sLengthMenu": "显示 _MENU_ 项结果",
						"sZeroRecords": "没有匹配结果",
						"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
						"sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
						"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
						"sInfoPostFix": "",
						"sSearch": "搜索:",
						"sUrl": "",
						"sEmptyTable": "无数据",
						"sLoadingRecords": "载入中...",
						"sInfoThousands": ",",
						"oPaginate": {
							"sFirst": "首页",
							"sPrevious": "上页",
							"sNext": "下页",
							"sLast": "末页"
						},
						"oAria": {
							"sSortAscending": ": 以升序排列此列",
							"sSortDescending": ": 以降序排列此列"
						}
					},
					destroy: true,	// 允许重建
					bProcessing:true,  // 表格数据过多处理时显示: sProcessing
					lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "全部"]],
					order: [],
					 //scrollY: 480,	// 滚动条
					 //scrollCollapse: true,
					 //jQueryUI: true,
					 stateSave: true,	// 保存最后一次分页信息、排序信息，当页面刷新，或者重新进入这个页面，恢复上次的状态。
					 stateDuration: 86400,	// 本地储存(0~更大)还是session储存(-1) 
				});
				
				// 关闭弹出框
				$('#modal-delete-' + session_id).iziModal('close');
				
				// 消息框
				toastr.success('成功关闭会话 "' + session_name + '"');
			}
		};
	}
	// 关闭rdp&vnc会话 - end
	
	
	// 锁定 webterminal 会话 - start
	createlockmodal = function (event) {
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id');
		var session_name = thisObj.attr("title");
		var type = thisObj.attr("type");
		// 生成弹出框，使用 \ 换行
		$("#modal-container").html('<div id="modal-lock-' + session_id + '" class="iziModal"><div class="container-fluid"><div class="row">\
									<div class="col-12 p-3"><h5 class="text-center mt-3">确定要锁定会话 "' + session_name + '" 吗？</h5></div><div class="col-6 p-3">\
									<button type="button" class="btn btn-block btn-secondary btn-flat" data-iziModal-close>取消</button></div><div class="col-6 p-3">\
									<a href="javascript:void(0)"><button type="button" class="btn btn-block btn-success btn-flat" id="' + session_id + '-yes" title="\
									' + session_name + '" type1="' + type + '" onclick="lockthis(this);">确定</button></a></div></div></div></div>');
		
		// 初始化弹出框
		$("#modal-lock-" + session_id).iziModal({
			iconClass: 'icon-announcement',
			width: 450,
			//padding: 10,
			overlayClose: false,	// 是否允许点击模态窗口的外部来关闭模态窗口。
			closeOnEscape: false, 	// 是否允许通过点击ESC键来关闭模态窗口。
		});
				
		// 打开弹出框
		$('#modal-lock-' + session_id).iziModal('open');
	}
	
	lockthis = function(event) {
		toastr.options.closeButton = true;
		toastr.options.showMethod = 'slideDown';
		toastr.options.hideMethod = 'fadeOut';
		toastr.options.closeMethod = 'fadeOut';
		toastr.options.timeOut = 4000;	
		toastr.options.extendedTimeOut = 0;	
		
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id').split("-")[0];
		var session_name = thisObj.attr("title");
		var type = thisObj.attr("type1");
		csrfmiddlewaretoken = '{{ request.COOKIES.csrftoken }}';
		var action_url;
		if (type == 'clissh') {
			action_url = "{% url 'webssh_api:session_clissh_lock' %}";
		} else if (type == 'webssh' || type == 'webtelnet' || type == 'webrdp' || type == 'webvnc' ) {
			action_url = "{% url 'webssh_api:session_lock' %}";
		};
		$.ajax({
			url: action_url,
			async: true,
			type: 'POST',
			dataType: 'json',
			data: {
				'csrfmiddlewaretoken': csrfmiddlewaretoken,
				'id': session_id,
			},
			timeout: 10000,
			cache: true,
			beforeSend: LoadFunction, //加载执行方法
			error: errFunction,  //错误执行方法
			success: succFunction, //成功执行方法
		});
		
		function LoadFunction() {
			//$("#aboutcontent").html('删除中...');
			//alert('删除中');
		};
		
		function errFunction() {
			// 关闭弹出框
			$('#modal-lock-' + session_id).iziModal('close');
			// 消息框
			toastr.error('锁定会话错误 "' + session_name + '"');
		};
		
		function succFunction(res) {
			if (res.code != 200) {
				// 关闭弹出框
				$('#modal-lock-' + session_id).iziModal('close');
				// 消息框
				toastr.error('锁定会话错误 "' + session_name + '": ' + res.err);
			} else {
				//$("#session-" + session_id).remove();find
				$($("#session-" + session_id).find('a[onclick="createlockmodal(this);"]')[0]).removeClass("btn-info").addClass("btn-primary");
				$($("#session-" + session_id).find('a[onclick="createlockmodal(this);"]')[0]).html('<i class="fas fa-lock-open"></i> 解锁');
				$($("#session-" + session_id).find('a[onclick="createlockmodal(this);"]')[0]).attr("onclick", "createunlockmodal(this);");
				
				// 关闭弹出框
				$('#modal-lock-' + session_id).iziModal('close');
				
				// 消息框
				toastr.success('成功锁定会话 "' + session_name + '"');
			}
		};
	}
	// 锁定 webterminal 会话 - end

	// 解锁 webterminal 会话 - start
	createunlockmodal = function (event) {
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id');
		var session_name = thisObj.attr("title");
		var type = thisObj.attr("type");
		// 生成弹出框，使用 \ 换行
		$("#modal-container").html('<div id="modal-unlock-' + session_id + '" class="iziModal"><div class="container-fluid"><div class="row">\
									<div class="col-12 p-3"><h5 class="text-center mt-3">确定要解锁会话 "' + session_name + '" 吗？</h5></div><div class="col-6 p-3">\
									<button type="button" class="btn btn-block btn-secondary" data-iziModal-close>取消</button></div><div class="col-6 p-3">\
									<a href="javascript:void(0)"><button type="button" class="btn btn-block btn-success" id="' + session_id + '-yes" title="\
									' + session_name + '" type1="' + type + '" onclick="unlockthis(this);">确定</button></a></div></div></div></div>');
		
		// 初始化弹出框
		$("#modal-unlock-" + session_id).iziModal({
			iconClass: 'icon-announcement',
			width: 450,
			//padding: 10,
			overlayClose: false,	// 是否允许点击模态窗口的外部来关闭模态窗口。
			closeOnEscape: false, 	// 是否允许通过点击ESC键来关闭模态窗口。
		});
				
		// 打开弹出框
		$('#modal-unlock-' + session_id).iziModal('open');
	}
	
	unlockthis = function(event) {
		toastr.options.closeButton = true;
		toastr.options.showMethod = 'slideDown';
		toastr.options.hideMethod = 'fadeOut';
		toastr.options.closeMethod = 'fadeOut';
		toastr.options.timeOut = 4000;	
		toastr.options.extendedTimeOut = 0;	
		
		var thisObj = $(event);	//js对象转jquery对象
		var session_id = thisObj.attr('id').split("-")[0];
		var session_name = thisObj.attr("title");
		var type = thisObj.attr("type1");
		csrfmiddlewaretoken = '{{ request.COOKIES.csrftoken }}';
		var action_url;
		if (type == 'clissh') {
			action_url = "{% url 'webssh_api:session_clissh_unlock' %}";
		} else if (type == 'webssh' || type == 'webtelnet' || type == 'webrdp' || type == 'webvnc' ) {
			action_url = "{% url 'webssh_api:session_unlock' %}";
		};
		$.ajax({
			url: action_url,
			async: true,
			type: 'POST',
			dataType: 'json',
			data: {
				'csrfmiddlewaretoken': csrfmiddlewaretoken,
				'id': session_id,
			},
			timeout: 10000,
			cache: true,
			beforeSend: LoadFunction, //加载执行方法
			error: errFunction,  //错误执行方法
			success: succFunction, //成功执行方法
		});
		
		function LoadFunction() {
			//$("#aboutcontent").html('删除中...');
			//alert('删除中');
		};
		
		function errFunction() {
			// 关闭弹出框
			$('#modal-unlock-' + session_id).iziModal('close');
			// 消息框
			toastr.error('解锁会话错误 "' + session_name + '"');
		};
		
		function succFunction(res) {
			if (res.code != 200) {
				// 关闭弹出框
				$('#modal-unlock-' + session_id).iziModal('close');
				// 消息框
				toastr.error('解锁会话错误 "' + session_name + '": ' + res.err);
			} else {
				//$("#session-" + session_id).remove();
				$($("#session-" + session_id).find('a[onclick="createunlockmodal(this);"]')[0]).removeClass("btn-primary").addClass("btn-info");
				$($("#session-" + session_id).find('a[onclick="createunlockmodal(this);"]')[0]).html('<i class="fas fa-lock"></i> 锁定');
				$($("#session-" + session_id).find('a[onclick="createunlockmodal(this);"]')[0]).attr("onclick", "createlockmodal(this);");

				// 关闭弹出框
				$('#modal-unlock-' + session_id).iziModal('close');
				
				// 消息框
				toastr.success('成功解锁会话 "' + session_name + '"');
			}
		};
	}
	// 解锁 webterminal 会话 - end
	
</script>
{% endblock js %}